package com.ruoyi.service.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.enums.YesNoEnum;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.service.user.entity.UserEntity;
import com.ruoyi.service.user.enums.UserStatusEnum;
import com.ruoyi.service.user.enums.UserTypeEnum;
import com.ruoyi.service.user.mapper.UserMapper;
import com.ruoyi.service.user.query.LaborPopQuery;
import com.ruoyi.service.user.query.LaborQuery;
import com.ruoyi.service.user.query.UserQuery;
import com.ruoyi.service.user.service.UserService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author Zane
 * @since 2025-09-27
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity>
    implements UserService {

  @Override
  public UserEntity getUserInfoByMobile(String phone, String userType) {
    //1 根据手机号查询用户信息
    final LambdaQueryWrapper<UserEntity> query = effectiveQuery().eq(UserEntity::getPhone, phone)
        .eq(UserEntity::getUserType, userType);
    return getOne(query);
  }

  @Override
  public List<UserEntity> getByUserNameOrMobile(String userName, String phone, String userType) {
    //
    return baseMapper.getByUserNameOrMobile(userName, phone, userType);
  }

  @Override
  public UserEntity getByUserNameAndPwd(String userName, String password, String userType) {
    final LambdaQueryWrapper<UserEntity> query = effectiveQuery().eq(UserEntity::getUserName,
            userName)
        .eq(UserEntity::getPassword, password)
        .eq(UserEntity::getUserType, userType);
    return getOne(query);
  }

  @Override
  public List<UserEntity> listLaborByQuery(LaborQuery laborQuery) {
    return this.baseMapper.listLaborByQuery(laborQuery);
  }

  @Override
  public List<UserEntity> selectLaborByQuery(LaborPopQuery popQuery) {
    final LambdaQueryWrapper<UserEntity> query = effectiveQuery()
        .eq(UserEntity::getUserType, UserTypeEnum.LABOR_USER.getCode())
        .eq(UserEntity::getBanFlag, YesNoEnum.NO.getCode())
        .orderByAsc(UserEntity::getUserId);
    if (StringUtils.isNotBlank(popQuery.getName())) {
      query.like(UserEntity::getName, popQuery.getName());
    }
    return list(query);
  }

  @Override
  public List<UserEntity> selectProjectLeader(LaborPopQuery popQuery) {
    final LambdaQueryWrapper<UserEntity> query = effectiveQuery()
        .eq(UserEntity::getUserType, UserTypeEnum.LABOR_USER.getCode())
        .eq(UserEntity::getBanFlag, YesNoEnum.NO.getCode())
        .eq(UserEntity::getAuthFlag, YesNoEnum.YES.getCode())
        .orderByAsc(UserEntity::getUserId);
    if (StringUtils.isNotBlank(popQuery.getName())) {
      query.like(UserEntity::getName, popQuery.getName());
    }
    return list(query);
  }

  @Override
  public Long getLaborCount() {
    return count(effectiveQuery()
        .eq(UserEntity::getUserType, UserTypeEnum.LABOR_USER.getCode()));
  }

  @Override
  public Long getLaborApproval() {
    return count(effectiveQuery()
        .eq(UserEntity::getUserType, UserTypeEnum.LABOR_USER.getCode())
        .eq(UserEntity::getStatus, UserStatusEnum.CHECKING.getCode())
    );
  }

  @Override
  public List<UserEntity> listUserNeedCheck(LaborQuery query) {
    return this.baseMapper.listUserNeedCheck(query);
  }

  @Override
  public Map<Long, UserEntity> userMapByIds(List<Long> userIds) {
    if (CollUtil.isEmpty(userIds)) {
      return new HashMap<>();
    }
    return Optional.ofNullable(this.listByIds(userIds)).orElseGet(ArrayList::new)
        .stream().collect(Collectors.toMap(UserEntity::getUserId, v -> v, (v1, v2) -> v1));
  }

  @Override
  public List<UserEntity> demanderUserManageList(UserQuery query,String creator) {
    final LambdaQueryWrapper<UserEntity> queryWrapper = effectiveQuery()
        .eq(UserEntity::getUserType, UserTypeEnum.LABOR_USER.getCode())
        .eq(UserEntity::getCreateBy,creator)
        .orderByAsc(UserEntity::getUserId);
    if (StringUtils.isNotBlank(query.getName())) {
      queryWrapper.like(UserEntity::getName, query.getName());
    }
    return list(queryWrapper);
  }

  private LambdaQueryWrapper<UserEntity> effectiveQuery() {
    return new LambdaQueryWrapper<UserEntity>()
        .eq(UserEntity::getDelFlag, YesNoEnum.NO.getCode());
  }
}
